<?php
function draw_javscript(){
	global $game;
	echo ' var real_move_num = '.($game['num_moves'] - 1).'; ';
	echo ' var move_num = '.($game['num_moves'] - 1).'; ';
	echo ' var moves = Array(';
		$html = '\'';
		foreach ($game['moves'] as $move){
			$html .= $move .'\',\'';
		}
		echo substr($html, 0, -3);
	echo '\'); ';
}

function printHTML_head(){
	echo '<?xml version="1.0"?>'."\n";
	echo '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">'."\n";
	echo '<html xmlns="http://www.w3.org/1999/xhtml">'."\n";
	echo '<html>';
	echo '<head><title>OwnYourPhone.com: Ringtone Download</title></head>';
	echo '<body>';
	echo '<div align="center"><img src="oyp.gif" alt="OwnYourPhone Logo" /></div>';
}
function printHTML_foot(){

	include('show_ads.php');
	echo '<p>&copy; '.date('Y').' <a href="http://www.OwnYourPhone.com">www.OwnYourPhone.com</a></p>';
    echo '</body>';
    echo '</html>';
}

// draws the board
function draw_board($board, $boardsize, $myturn){
	global $game;
	global $bisher;
	global $mstonex;
	global $mstoney;
	global $game;

	$empty_board = create_empty_board($boardsize);

	$color = strtolower(substr($game['player']['color'], 0, 1));

	echo '<div id="board" style="width:'.(($boardsize + 2) * 25).'px;height:'.(($boardsize + 2) * 25).'px;">';
	echo "\n";
	echo "<div class=\"corner {$game['player']['color']}\"></div>";

	for ($x = 0; $x < $boardsize; ++$x){
		$letter = (8 > $x) ? chr($x + 65) : chr($x + 66); // skip the letter I
		echo '<div class="top">' . $letter . "</div>";
	}

	echo "<div class=\"corner {$game['player']['color']}\"></div>";

	for ($y = 0; $y < $boardsize; ++$y){
		echo "\n";
		echo '<div class="left">' . ($boardsize - $y) . '</div>';

		for ($x = 0; $x < $boardsize; ++$x){
			if ($board[$y][$x] == 'b'){
				$image = 'b.gif';
				$alt   = 'X';

				if (($mstoney == $y) && ($mstonex == $x)){
					$image = 'bm.gif';
					$alt   = '(X)';
				}
			}
			else if ($board[$y][$x] == 'w'){
				$image = 'w.gif';
				$alt   = 'O';

				if (($mstoney == $y) && ($mstonex == $x)){
					$image = 'wm.gif';
					$alt   = '(O)';
				}
			}
			else{
				$image = '';
				$alt   = '';
			}

			$image_html = ('' != $image) ? "<img src=\"image/{$image}\" alt=\"{$alt}\" />" : '';

			if (is_legal_move($y, $x, $color) && $game['player']['move'] && ! is_ko($y, $x, $color)
				&& ( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator'])))
			{
				$move_html = " onclick=\"make_move({$x},{$y});\"";
			}
			else
			{
				$move_html = '';
			}
			
			$ychar = strtolower(xtoalt($y, true));
			$xchar = strtolower(xtoalt($x, true));

			echo "<div id=\"s{$ychar}{$xchar}\" class=\"{$empty_board[$y][$x]}\"{$move_html}>{$image_html}</div>";
		}

		echo '<div class="right">' . ($boardsize - $y) . '</div>';
	}

	echo "\n";
	echo "<div class=\"corner {$game['player']['color']}\"></div>";

	for ($x = 0; $x < $boardsize; ++$x)
	{
		$letter = (8 > $x) ? chr($x + 65) : chr($x + 66); // skip the letter I
		echo '<div class="top">' . $letter . '</div>';
	}

	echo "<div class=\"corner {$game['player']['color']}\"></div>\n</div>\n";
}


function draw_interface($wprisoners, $bprisoners, $history, $bpname, $wpname, $myturn, $passes, $resign)
{
	global $color;
	global $mycolor;
	global $game;
	global $board;
	global $bhistory;

	$boardsize = $game['board_size'];
	$freedoms = array( );

	echo '<div id="interface">';

	if ($resign == 1)
	{
		echo '<div class="notice">Black Player Has Resigned! White Player wins the game!</div>';
	}
	else if ($resign == 2)
	{
		echo '<div class="notice">White Player Has Resigned! Black Player wins the game!</div>';
	}
	else if ($passes == 2)
	{
		echo '<div class="notice">Both Players Have Passed! Please Count Your Scores To Determine The Winner.</div>';
	}
	else if ($myturn && $resign == 0 && $passes < 2 && $bhistory != true)
	{
		echo '<div class="notice">It\'s Your Turn! Make Your Move.  You\'re playing '.$game['player']['color'].'</div>';
	}
	else
	{
		if (( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator'])) && (true != $bhistory))
		{
			echo '<div class="notice">It\'s Not Your Turn! Please Wait.</div>';
		}
		else
		{
			echo '<div class="notice">Your are only a spectator! You are not allowed to interact.</div>';
		}
	}

	echo '<hr />';

	if ($mycolor == 'b')
	{
		echo '<b>';
	}

	echo 'Black Player: ', $bpname, '<br />';

	if ($mycolor == 'b')
	{
		echo '</b>';
	}

	if ($mycolor == 'w')
	{
		echo '<b>';
	}

	echo 'White Player: ', $wpname, '<br /><br />';

	if ($mycolor == 'w')
	{
		echo '</b>';
	}

	echo 'Number Of Stones Captured By Black: ', $bprisoners, '<br />';
	echo 'Number Of Stones Captured By White: ', $wprisoners, '<br /><br />';

	print_history($history);

	echo '<br />';

	if ($myturn && $resign == 0 && $passes < 2)
	{
		echo ' <a href="?submit=pass">pass</a> ';
		echo ' <a href="?submit=resign">resign</a> ';
	}

#	echo ' <a href="?sgf=1">Download SGF</a> ';
	echo ' <a href="index.php">home</a> ';
	echo '</div>';
}


function print_history($history)
{
	global $game;

	if (0 == $game['num_moves'])
	{
		return false;
	}

	echo '<table id="history"><caption>history</caption>';
	echo '<tr>';

	if (isset($_GET['until']))
	{
		$until = $_GET['until'];
	}

	$j = 0;
	for ($i = 0; $i < $game['num_moves']; ++$i)
	{
		$border = ((isset($until) && ($i == ($until - 1))) || ( ! isset($until) && ($i == ($game['num_moves'] - 1))))  ? ' until' : '';

		if ((0 != $i) && (0 == ($i % 12)))
		{
			echo "\n</tr><tr>";
		}

		if ($i % 2 == 0)
		{
			if ($game['handicap'] == 0)
			{
				echo "<td id=\"h{$i}\" class=\"bl{$border}\">";
			}
			else
			{
				echo "<td id=\"h{$i}\" class=\"wh{$border}\">";
			}
		}
		else
		{
			if ($game['handicap'] == 0)
			{
				echo "<td id=\"h{$i}\" class=\"wh{$border}\">";
			}
			else
			{
				echo "<td id=\"h{$i}\" class=\"bl{$border}\">";
			}
		}

		echo '<a href="#" onclick="go_to_move('.$i.');return false;">'.$history[$i].'</a>';
		echo '</td>';
	}
	
	// fill the rest of the blank spaces
	for ($i = $i; 0 < ($i % 12); ++$i)
	{
		echo '<td class="blank"></td>';
	}

	echo '</tr><tr>';

	echo '<td><a href="#" onclick="prev_move( );return false;">&laquo;</a></td>';

	for ($i = 0; $i < 10; ++$i)
	{
		echo '<td class="blank"></td>';
	}

	echo '<td><a href="#" onclick="next_move( );return false;">&raquo;</a></td>';

	echo '</tr>';
	echo '</table>';
}


// this function only exists to make it easy to disable
// otherwise it would be hard-coded
function draw_chatbox($data)
{
	global $game;

	echo '<div class="chatwrapper">
	<form action="play.php" method="post">
	<div class="chatbox">
	<table class="chat">
		';
	
	$i = 0;	
	foreach ($data as $item)
	{
		$alt = (0 == ($i %2)) ? ' class="alt"' : '';
		$private = ('Yes' == $item['c_private']) ? ' class="private"' : '';

		echo "<tr{$alt}>
			<th{$private}>{$item['p_username']}:</th><td>{$item['c_message']}</td>
		</tr>";
		
		++$i;
	}
	
	echo '
	</table>
	</div>
	<input type="hidden" name="player_id" value="'.$_SESSION['player_id'].'" />
	<input type="text" name="message" size="60" />
	';
	
	// spectators don't need the private box
	if ( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator']))
	{
		echo '<label for="private"><input type="checkbox" name="private" id="private" value="Yes" />Private</label>';
	}
	
	echo '
	<input type="submit" name="submit" value="send" />
	</form>';
	
	if ( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator']))
	{
		if (0 == $game['private_chat_box'])
		{
			echo 'The chat box for this game is currently set as <strong>public</strong>.<br />
			This means that other players (not just you and your opponent) may post
			in the chat box.
			<form action="play.php" method="post" style="display:inline;">
			<input type="hidden" name="private" value="'.$_SESSION['player_id'].'" />
			<input type="submit" name="submit" value="change to private" />
			</form>
			';
		}
		else if ($_SESSION['player_id'] == $game['private_chat_box'])
		{
			echo 'You have set the chat box for this game as <strong>private</strong>.<br />
			This means that only you and your opponent can post
			messages to this chat box.  Because you made it private, only you can set
			the chat box back to public.
			<form action="play.php" method="post" style="display:inline;">
			<input type="hidden" name="private" value="0" />
			<input type="submit" name="submit" value="change to public" />
			</form>
			';
		}
		else
		{
			echo 'Your opponent has set the chat box for this game as <strong>private</strong>.<br />
			If you wish it otherwise, discuss it with your opponent and then it is
			up to them if they wish to make it public.
			';
		}
	
		echo '<br />';
	}
	else{
		if (0 != $game['private_chat_box']){
			echo 'This chat box is set to <strong>private</strong>.';
		}
	}

	echo '</div>';	
}


function screen($board, $boardsize, $doit, $wprisoners, $bprisoners, $history, $bpname, $wpname, $passes, $resign)
{
	echo '</head>';
	echo '<body>';
	echo '<table>';
	echo '<tr>';
	echo '<td>';
	draw_board($board, $boardsize, $doit);
	echo '</td>';
	echo '<td>';
	draw_interface($wprisoners, $bprisoners, $history, $bpname, $wpname, $doit, $passes, $resign);
	echo '</td>';
	echo '</tr>';
	echo '</table>';
	echo '</body>';
	echo '</html>';
}


// load a game and send the user an sgf file from it
function write_sgf($game_id){
	if (file_exists($filename) && is_readable($filename)){
		$name = substr($filename, 0, strlen($filename) - 4);
		$sgf = $name . '.sgf';
		$fp = fopen($filename, 'r');
		$bpname = fgets($fp, 1024);
		$wpname = fgets($fp, 1024);
		$boardsize = intval(fgets($fp, 1024));
		$handicap = intval(fgets($fp, 1024));
		$status = intval(fgets($fp, 1024));
		$fo = "(;SZ[{$boardsize}]PC['go.inc.sf.net']HA[{$handicap}]PB[" . trim($bpname) . "]PW[" . trim($wpname) . "]";

		while ( ! feof($fp)){
			$temp = trim(fgets($fp, 1024));

			if ($temp != ''){
				for ($i = 0; $i < strlen($temp); $i = $i + 3){
					if (substr($temp, $i, 1) != 'r' && substr($temp, $i, 1) != 'e'){
						if (substr($temp, $i + 1, 2) == 'xx'){
							if (substr($temp, $i, 1) == 'w'){
								$fo = $fo . ';W[';
							}
							else{
								$fo = $fo . ';B[';
							}
						}
						else if (substr($temp, $i + 1, 2) == 'zz'){
							if (substr($temp, $i, 1) == 'w'){
								$fo = $fo . ';RE[B+Resign';
							}
							else{
								$fo = $fo . ';RE[W+Resign';
							}
						}
						else{
							if (substr($temp, $i, 1) == 'w'){
								$fo = $fo . ';W[';
							}
							else{
								$fo = $fo . ';B[';
							}
							$fo = $fo . substr($temp, $i + 2, 1);
							$fo = $fo . substr($temp, $i + 1, 1);
						}
						$fo = $fo . ']';
					}
				}
			}
		}

		$fo = $fo . ')';
		fclose($fp);

		header('Content-Type: text/sgf');
		header("Content-Disposition: attachment; filename={$sgf}");
		echo $fo;

		return true;
	}
	else{
		echo 'The game id given :' . $game_id . ' does not exist or is not readable!<br />';
		exit;
	}
}


// debug function
function call($var = '^^k8)SJ2di!U'){
	if (defined('DEBUG') && ! DEBUG){
		return false;
	}

	if ('^^k8)SJ2di!U' === $var){
		echo '<span style="font-weight:bold;background:white;color:red;">*****</span>';
	}
	else{
		// begin output buffering so we can escape any html
		ob_start();

		if (is_string($var) && isset($GLOBALS[$var])){
			echo '$' . $var . ' = ';
			$var = $GLOBALS[$var];
		}
		
		if (is_bool($var) || is_null($var)){
			var_dump($var);
		}
		else{
			print_r($var);
		}

		// end output buffering and output the result
		$contents = htmlentities(ob_get_contents( ));
		ob_end_clean();
		
		echo '<pre style="background:#FFF;color:#000;font-size:larger;">'.$contents.'</pre>';
	}	
}


	


?>
